package com.apache.rpc.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.manager.InterfaceRegisterManager;
import com.apache.rpc.service.impl.helper.CacheTactics;

public class InterfaceRegisterManagerImpl implements InterfaceRegisterManager {

	protected IDao interfaceRegisterDao;
	protected final String entityName = "com.apache.rpc.entity.InterfaceRegister";

	protected CacheTactics interfaceRegisterCacheTactics;

	public void setInterfaceRegisterDao(IDao interfaceRegisterDao) {
		this.interfaceRegisterDao = interfaceRegisterDao;
	}

	public String saveInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
		InterfaceRegister interfaceRegister = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		//String cacheKey = "";
		String cacheKey = "interfaceRegister_" + infoId;
		interfaceRegister.setRegisterId(infoId);
		MethodParam param = new MethodParam("InterfaceRegister", cacheKey, "", entityName);
		param.setVaule(interfaceRegister);
		if (interfaceRegisterDao.insert(param)) {
			interfaceRegisterCacheTactics.addInterfaceRegisterCache(interfaceRegister);
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
		InterfaceRegister interfaceRegister = vo.getObj();
		if (Validator.isNotNull(interfaceRegister.getRegisterId())) {
			//String cacheKey = "";
			String cacheKey = "interfaceRegister_" + interfaceRegister.getRegisterId();
			MethodParam param = new MethodParam("InterfaceRegister", cacheKey, "", entityName);
			param.setVaule(interfaceRegister);
			boolean mark = interfaceRegisterDao.edit(param);
			if (mark) {
				interfaceRegisterCacheTactics.editInterfaceRegisterCache(interfaceRegister);
			}
			return mark;
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<InterfaceRegister> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		//String cacheKey = "";
		String cacheKey = "interfaceRegister_" + infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		InterfaceRegister info = (InterfaceRegister) interfaceRegisterDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		boolean flag = false;
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("InterfaceRegister");
			flag = interfaceRegisterDao.edit(param);
		} else {
			param.setParams("registerId", infoId);
			param.setDelete(true);
			flag = interfaceRegisterDao.delete(param);
		}
		if (flag) {
			interfaceRegisterCacheTactics.delInterfaceRegisterCache(infoId, info.getBeanId());
		}
		return flag;
	}

	public Object getInfoById(ParamsVo<InterfaceRegister> vo) {
		String infoId = vo.getInfoId();
		//String cacheKey = "";
		String cacheKey = "interfaceRegister_" + infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return interfaceRegisterDao.selectById(param);
	}

	public Object execute(ParamsVo<InterfaceRegister> vo) {
		String key = vo.getMethodKey();
		if ("initCache".equals(key)) {
			interfaceRegisterDao.initSystemCache();
		}
		return null;
	}

	public Page getPageInfo(ParamsVo<InterfaceRegister> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = interfaceRegisterDao.pageSelect(param);
		return page;
	}

	@SuppressWarnings("unchecked")
	public List<InterfaceRegister> getList(ParamsVo<InterfaceRegister> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return interfaceRegisterDao.select(param);
	}

	public long countInfo(ParamsVo<InterfaceRegister> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return interfaceRegisterDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<InterfaceRegister> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(interfaceRegisterDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		InterfaceRegister interfaceRegister = vo.getObj();
		if (Validator.isEmpty(interfaceRegister))
			return param;
		if (Validator.isNotNull(interfaceRegister.getBeanId())) {
			sb.append(" and beanId = :beanId");
			param.setParams("beanId", interfaceRegister.getBeanId());
		}
		if (Validator.isNotNull(interfaceRegister.getMethodName())) {
			sb.append(" and methodName = :methodName");
			param.setParams("methodName", interfaceRegister.getMethodName());
		}
		if (Validator.isNotNull(interfaceRegister.getBeanName())) {
			sb.append(" and beanName like '%" + interfaceRegister.getBeanName() + "%'");
			param.setParams("beanName", "'%" + interfaceRegister.getBeanName() + "%'");
		}
		if (Validator.isNotNull(interfaceRegister.getFlag())) {
			sb.append(" and flag = :flag");
			param.setParams("flag", interfaceRegister.getFlag());
		}

		param.setSqlStr(sb.toString());
		return param;
	}

	public void setInterfaceRegisterCacheTactics(CacheTactics interfaceRegisterCacheTactics) {
		this.interfaceRegisterCacheTactics = interfaceRegisterCacheTactics;
	}
}
